package com.yl.module.common.service.impl;

import com.yl.module.common.entity.Student;
import com.yl.module.common.dao.StudentDao;
import com.yl.module.common.service.IStudentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.stream.IntStream;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Administrator
 * @since 2020-03-06
 */

@Service
@CacheConfig(cacheNames = "myCache")
public class StudentServiceImpl extends ServiceImpl<StudentDao, Student> implements IStudentService {

    private static final Logger log = LoggerFactory.getLogger(StudentServiceImpl.class);

    @Resource
    private StudentDao dao;


    @Override
    @Cacheable(key = "#id")
    public Student findOne(int id) {
        System.out.println("正在查询数据库...");
        log.info("正在查询数据库...");
        return dao.selectById(id);
    }

    @Override
    @CachePut(key = "#s.id")
    public int updateStudent(Student s) {
        return dao.updateById(s);
    }

    @Override
    //@CacheConfig => @CacheEvict(value = "myCache")
    @CacheEvict(key = "#id")
    public int deleteStudent(int id) {
        return dao.deleteById(id);
    }

    @Override
    @CacheEvict(value = "#cacheName", allEntries = true)
    public void clearCache(String cacheName) {}

    @Override
    @CacheEvict(cacheNames = {"testCache", "myCache", "studentCache"}, allEntries = true)
    public void clearAllCache() {}
}
